//
//  XHRCategoryView.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/4.
//  Copyright © 2016年 xuhongru. All rights reserved.
//

#import "XHRCategoryView.h"
#import "XHRVerticalButton.h"

@interface XHRCategoryView()

/**顶部标签条*/
@property(nonatomic,weak)UIView *topView;

/**底部放按钮的view*/
@property(nonatomic,weak)UIView *bottomView;

@end
static const NSInteger colum = 3;
static const NSInteger totalCount = 6;
static const CGFloat topViewH = 40.0;
static const CGFloat margin = 0.5;
@implementation XHRCategoryView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initSettings];
        //创建6个按钮
        [self setupButtons];
    }
    return self;
}
#pragma mark -
#pragma mark - initSettings
- (void)initSettings
{
    self.backgroundColor = [UIColor lightGrayColor];
}
#pragma mark -
#pragma mark - layoutSubviews
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.topView makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.offset(0);
        make.height.equalTo(topViewH);
    }];
    [self.bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.offset(0);
        make.top.equalTo(self.topView.bottom).offset(0);
    }];
    CGFloat buttonW = self.xhr_Width / colum - margin;
    CGFloat buttonH = (self.xhr_Height - topViewH) / (totalCount / colum) - margin;
    for (NSInteger i = 0; i < totalCount; i++) {
        UIButton *button = self.bottomView.subviews[i];
        button.frame = CGRectMake(margin + i % colum * (buttonW + margin),margin + i / colum * (buttonH + margin), buttonW , buttonH);
    }
}
#pragma mark -
#pragma mark - 监听按钮的点击
- (void)didClickCategoryButton:(UIButton *)sender
{

    if (![sender.currentTitle isEqualToString:@"公益"]) {
        NSUInteger index = [self.bottomView.subviews indexOfObject:sender];
        if (self.categoryViewBlock) {
            self.categoryViewBlock(index,sender.currentTitle);
        }
    }else{
   
        if (self.syblock) {
            
            self.syblock();
        }
 
        }
    
    
    
    }
    

#pragma mark -
#pragma mark - setupsetupButtons
- (void)setupButtons
{
    NSArray *buttonTitleArray = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科",@"公益"];
    NSArray *buttonImageArray = @[@"zl",@"xyg",@"sj",@"xy",@"gt",@"gy"];
    
    for (NSInteger i = 0; i < totalCount; i++) {
        UIButton *button = [XHRVerticalButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:buttonImageArray[i]] forState:UIControlStateNormal];

        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didClickCategoryButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:button];
    }
    
}
#pragma mark -
#pragma mark - 懒加载底部放按钮的view
- (UIView *)bottomView
{
    if (!_bottomView) {
        UIView *view = [[UIView alloc]init];
        [self addSubview:view];
        
        _bottomView = view;
    }
    return _bottomView;
}
#pragma mark -
#pragma mark - 懒加载顶部标题view
- (UIView *)topView
{
    if (!_topView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
      
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.enabled = NO;
        [button setImage:[UIImage imageNamed:@"yj"] forState:UIControlStateNormal];
        [button setTitle:@"名医通" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
        [view addSubview:button];
        
        [button makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.leading.offset(0);
            make.width.equalTo(85);
        }];
        
        _topView = view;
    }
    return _topView;
}
@end
